package org.inspursc.s0517.health.common.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.inspursc.s0517.health.common.dto.TempFilePathDTO;
import org.inspursc.s0517.health.common.entity.SysUser;
import org.inspursc.s0517.health.common.service.FileDealService;
import org.inspursc.s0517.health.common.util.SpringSecurityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author B_Dream
 * @date 2018/5/28 23:07
 */

@Service
@Slf4j
public class FileDealServiceImpl implements FileDealService {

    @Value("${uploadPath}")
    private String uploadDir;

    @Override
    public List<TempFilePathDTO> uploadsBatchFiles(HttpServletRequest request) throws Exception {


        List<TempFilePathDTO> savePathList = new ArrayList<>();
        try {
            List<MultipartFile> files = ((MultipartHttpServletRequest) request)
                    .getFiles("file");

            SysUser user = SpringSecurityUtils.getCurrentUser(request);

            Integer id = user.getId();
            //如果目录不存在，自动创建文件夹
            File dir = new File(uploadDir + "/" + id);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            for (MultipartFile file : files) {
                if (file != null) {
                    //截取文件后缀名
                    String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                    //新建文件名
                    String fileName = UUID.randomUUID() + suffix;
                    String uploadPath = uploadDir + id + "/" + fileName;
                    FileOutputStream out = new FileOutputStream(uploadPath);
                    TempFilePathDTO tempFile = new TempFilePathDTO();
                    tempFile.setUrl("/" + uploadPath);
                    savePathList.add(tempFile);
                    out.write(file.getBytes());
                    out.flush();
                    out.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("文件上传异常");
        }
        return savePathList;
    }

    @Override
    public TempFilePathDTO uploadsFile(MultipartFile file, HttpServletRequest request) {

        SysUser user = SpringSecurityUtils.getCurrentUser(request);
        String logo = "logo";
//            Integer id = user.getId();
        //如果目录不存在，自动创建文件夹
        File dir = new File(uploadDir + "/" + logo);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        TempFilePathDTO tempFile = new TempFilePathDTO();
        if (file != null) {
            try {
                //截取文件后缀名
                String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
                //新建文件名
                String fileName = UUID.randomUUID() + suffix;
                String uploadPath = uploadDir + logo + "/" + fileName;
                FileOutputStream out = new FileOutputStream(uploadPath);
                tempFile.setUrl("/" + uploadPath);
                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return tempFile;

    }
}
